package com.jeeplus.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CreateBucketRequest;
//import lombok.Getter;
//import lombok.Setter;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Slf4j
@Configuration
//@ConfigurationProperties(prefix = "oss")
//@Setter
//@Getter
public class OssConfig {

    private String endpoint = "oss-cn-beijing.aliyuncs.com";

    private String accessKeyId = "LTAI4GECxViwuWBwFQKrpx2A";

    private String accessKeySecret = "BPXSLxxDBVpQDA9fDxkDzJpcJKy5dT";

    private String bucket = "ptwang";

    private String serverPath = "http://ptwang.oss-cn-beijing.aliyuncs.com/";

    @Bean
    public OSS oss(){
        try{
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            boolean isExist = ossClient.doesBucketExist(bucket);
            if (isExist) {
                System.out.println("Bucket already exists.");
            } else {
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucket);
                ossClient.createBucket(createBucketRequest);
            }
            return ossClient;
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("Oss server invalid");

        return null;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String getServerPath() {
        return serverPath;
    }

    public void setServerPath(String serverPath) {
        this.serverPath = serverPath;
    }
}
